#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>
#include <QSerialPort>
#include <QMutex>
#include <QTimer>
#include "ettadatastruct.h"


class Controller : public QSerialPort
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = nullptr);
    ~Controller();

    QString make_communicate_packet(CMD_TYPE cmd_type, int value1, int value2, int value3, int value4, int value5);
    void decodeBuffer(QString buffer);

    void EnablecommTimeOut();
    void DisablecommTimeOut();

protected:
    CMD_TYPE xcmd_type;

signals:
    void progressChangeSignal(int val);
    void dataReceivedSignal(QString data);
    void expResultPassSignal(const HostReply &);
    void expResultFailSignal(const HostReply &);

    //self check
    void selfCheckResultPassSignal(const HostReply &);
    void selfCheckResultFailSignal(const HostReply &);

    //pwd and flw check
    void pwdflwCheckResultSignal(const HostReply &);

    //SHK_Result
    void shkCheckResultSignal(const HostReply &);

    //Start Experiment
    void experimentStartSignal(const HostReply &);

    // host version
    void hostVersionSignal(const QString &);

    // liquid detected
    void liquidDetectedSignal();

    // motor calibrate done
    void motorCalibDoneSignal(const HostReply &);

public slots:
    void openSerialPortSlot(const QString & name);
    void sendPortCmdSlot(ComCmd cmd);
    //Power on Self Test
    void POST();
    void watchPower();
    void readSerialPortSlot();
    void updateProgressSlot();
    void updatecommTimeOutSlot();
    void updatecommRecvSlot();
    void lightLedSlot(const LedType &led);

private:
    int asciiListToInt(const QString &asciiList);

private:
    QMutex mutex;
    QString dataRead;
    QTimer * progressTimer;
    QTimer * commTimeOutTimer;
    QTimer * commRecvTimer;
    QByteArray byteBuffRecv;

    HostReply selftestReply;
    bool commOpened;
    bool isCheckPass;

    QString passStr;
    int progressVal;
    int commTimeOutVal;
    int progressCount;
};

#endif // CONTROLLER_H
